package com.demo.cms.action.manage;

import java.util.ArrayList;
import java.util.List;

import org.springframework.context.i18n.LocaleContextHolder;

import com.demo.Constants;
import com.demo.cms.model.Channel;
import com.demo.cms.service.ChannelService;
import com.demo.core.dao.base.support.ResultSearchResult;
import com.demo.core.dao.base.support.SearchArgumentSupport;
import com.demo.core.model.Config;
import com.demo.core.model.Site;
import com.demo.core.model.Template;
import com.demo.core.service.base.ConfigService;
import com.demo.core.service.base.TemplateService;
import com.demo.security.annotation.function.Function;
import com.demo.webapp.action.PageAction;
import com.demo.webapp.action.TemplateProvider;
import com.demo.webapp.context.MultisiteContextHolder;
import com.opensymphony.xwork2.interceptor.annotations.InputConfig;

/**
 * 频道Action
 * 频道是CMS的基础
 * 一个CMS系统的可能有1-n个频道组成
 * 频道将栏目归类,思想来源于磁盘分区与文件夹
 * 频道将不同的栏目做出相应的隔离,这样方便管理栏目
 * 另外,频道的增加易于扩展整个CMS系统
 * 比如不使用i18n作为多语言设计时,将频道看做是一个天生的语言隔离数据
 * 多语言问题将转换为多频道。一个频道管理一种语言
 * @author ryuu.kk
 *
 */
@Function(id = 104000, pid = 10, code = "manage.channel")
public class ChannelAction extends PageAction implements TemplateProvider {

	/**
	 * 版本序列化ID
	 */
	private static final long serialVersionUID = -7177987904990060550L;

	/**
	 * 模板模型
	 */
	private Template template;
	
	/**
	 * 频道ID
	 */
	private Long id;

	/**
	 * 频道模型
	 */
	private Channel channel;
	
	/**
	 * 频道列表
	 */
	private List<Channel> channelList;
	
	/**
	 * 频道服务
	 */
	private ChannelService channelService;
	
	/**
	 * 模板服务
	 */
	private TemplateService templateService;
	
	/**
	 * 配置服务
	 */
	private ConfigService configService;
	
	/**
	 * 模板列表
	 */
	private List<Template> templateList;
	
	/**
	 * 国际化配置
	 */
	private Config configI18N;
	
	/**
	 * tab索引
	 */
	private int idx = 1;
	
	/**
	 * 是否是默认频道
	 */
	private boolean isDefault;
	
	/**
	 * 频道列表
	 * 查询站点所有频道
	 * @return String
	 */
	@Function(id = 104010, pid = 104000, code = "manage.channel.list")
	public String list() {

		// 取得语言
		String i18n = LocaleContextHolder.getLocale().toString();
		if (log.isDebugEnabled()) {
			log.debug("Current-Language is " + i18n);
		}
		// 取得站点ID
		Site site = MultisiteContextHolder.getSite();
		Long siteId = site.getId();
		
		//取得频道模板列表
		this.templateList = getTemplate(siteId);
		if (templateList == null || templateList.size() == 0) {
			addActionMessage("频道模板不存在,请先创建模板.");
		}
		// 取得语言配置
		this.configI18N = configService.query(siteId, Constants.CONFIG_I18N);
		
		SearchArgumentSupport args = getArgumentSupport();
		ResultSearchResult<Channel> result = channelService.search(siteId, args.getFirstResult(), args.getMaxResults());
		this.setResultSupport(result);
		this.channelList = new ArrayList<Channel>(result.getResults());
		return SUCCESS;
	}
	
	/**
	 * 创建频道信息
	 * @return String
	 */
	@Function(id = 104030, pid = 104000, code = "manage.channel.save")
	@InputConfig(methodName = "list")
	public String save() {
		// 取得站点ID
		Site site = MultisiteContextHolder.getSite();
		Long siteId = site.getId();
		if (channelService.find(channel.getPath()) != null) {
			addActionError(getText("channel.path.existed"));
			return INPUT;
		}
		
		channel.setSiteId(siteId);
		
		id = channelService.save(channel);
		if (isDefault) {
			// 设置其他站点为一般状态
			channelService.setDefault(id, siteId);
		} 
		addActionMessage(getText("channel.added", "频道已创建.", channel.getTitle()));
		return CALLBACK;
	}
	/**
	 * 频道的信息更新
	 * @return String
	 */
	@Function(id = 104050, pid = 104000, code = "manage.channel.update")
	@InputConfig(methodName = "list")
	public String update() {
		
		// 判断path是否重复
		Channel channelTmp = channelService.find(channel.getPath());
		// 存在path,并且与原始path不同
		if (channelTmp != null && !channelTmp.getPath().equals(channel.getPath())) {
			addActionError(getText("channel.path.existed"));
			return INPUT;
		}
		channelService.update(id, channel.isEnabled(), Channel.STATUS_NORMAL);
		if (isDefault) {
			// 设置默认站点
			channelService.setDefault(id, getSite().getId());
		}
		channelService.update(id, channel.getTitle(), channel.getPath(), channel.getTemplateId(), channel.getI18n());
		
		addActionMessage(getText("channel.updated"));
		return CALLBACK;
	}
	
	/**
	 * 删除频道信息
	 * @return String
	 */
	@Function(id = 104060, pid = 104000, code = "manage.channel.delete")
	public String delete() {
		
		channelService.delete(id);
		
		addActionMessage(getText("channel.deleted"));
		return CALLBACK;
	}
	
	/**
	 * 预览频道信息
	 * @return String
	 */
	@Function(id = 104070, pid = 104000, code = "manage.channel.review")
	public String review() {
		
		// 取得渲染模板
		template = templateService.find(channel.getTemplateId());
		// 使用自定义渲染
		return "selfRender";
	}
	
	/**
	 * 取得频道模板
	 * @return List<Template>
	 */
	private List<Template> getTemplate(Long siteId) {
		// 取得文章模板列表,不包含模板内容
		return templateService.findList(siteId, Constants.TYPE_CHANNEL, false);
	}
	
	// ~~ settings ----------------------------------------------------------------------
	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public Channel getChannel() {
		return channel;
	}

	public void setChannel(Channel channel) {
		this.channel = channel;
	}

	public List<Channel> getChannelList() {
		return channelList;
	}

	public List<Template> getTemplateList() {
		return templateList;
	}

	public void setTemplateService(TemplateService templateService) {
		this.templateService = templateService;
	}

	public void setChannelService(ChannelService channelService) {
		this.channelService = channelService;
	}

	public void setConfigService(ConfigService configService) {
		this.configService = configService;
	}

	public Config getConfigI18N() {
		return configI18N;
	}

	public int getIdx() {
		return idx;
	}

	public void setIdx(int idx) {
		this.idx = idx;
	}

	public void setDefault(boolean isDefault) {
		this.isDefault = isDefault;
	}

	@Override
	public Template getTemplateModel() {
		return template;
	}
}
